
import 'dart:collection';

import 'package:flutter_wan_android/data/article/article_entity.dart';
import 'package:flutter_wan_android/data/article/article_repository.dart';
import 'package:flutter_wan_android/data/banner/banner_entity.dart';
import 'package:flutter_wan_android/data/banner/banner_repository.dart';
import 'package:flutter_wan_android/data/logic_result.dart';
import 'package:flutter_wan_android/data/page_entity.dart';
import 'package:flutter_wan_android/feature/core/base_view_model.dart';

class HomeViewModel extends BaseViewModel {
  final BannerRepository _bannerRepository;
  final ArticleRepository _articleRepository;

  HomeViewModel(this._bannerRepository,this._articleRepository);

  List<BannerEntity> _bannerList = [];

  UnmodifiableListView<BannerEntity> get bannerList => UnmodifiableListView(_bannerList);


  void requestBannerList() {
    _bannerRepository.requestList().listen((result) {
      switch (result) {
        case Ok<List<BannerEntity>>(:final data):
          _bannerList = data;
          notifySuccess();
        case Fail(:final code, :final msg):
          notifyError(msg, code: code);
      }
    });
  }

  /// ---------  未封装版本，演示分页列表的使用 ---------
  /// emm 感觉分页状态 不该在viewModel中处理，自定义一个widget，分页状态内置在组件中 感觉更合理
  /// 对外暴露 onRefresh 和 onLoadMore 方法

  int _pageIndex = 0;
  ListStatus listStatus = ListStatus.empty();
  final List<ArticleEntity> _articleList = [];
  UnmodifiableListView<ArticleEntity> get articleList => UnmodifiableListView(_articleList);

  void requestArticleList(bool refresh) async {
    if(refresh){
      //刷新 重置页码
      _pageIndex = 0;
    } else {
      //加载更多 页码+1，列表底部显示加载中
      _pageIndex++;
      listStatus = ListStatus.loading();
      notifyListeners();
    }

    final result = await _articleRepository.requestList(_pageIndex);
    switch(result){
      case Ok<PageEntity<ArticleEntity>>(:final data):
        final list = data.list;
        if(data.curPage == 1){
          _articleList.clear();
          //第一页
          if(list.isEmpty){
            //空数据
            listStatus = ListStatus.empty();
          } else {
            _articleList.addAll(list);
            listStatus = ListStatus.success();
          }
        } else {
          //加载更多 data.over == true 表示所有数据加载完毕 没有下一页了
          listStatus = data.over ? ListStatus.completed():ListStatus.success();
          _articleList.addAll(list);

        }
        //模拟错误情况，配合UI演示错误情况
        if(data.curPage == 2){
          listStatus = ListStatus.error();
        }
        notifyListeners();
      case Fail(:final code,:final msg):
        listStatus = ListStatus.error();
        if(!refresh){
          _pageIndex--;
        }
    }
  }
}

sealed class ListStatus {
  const ListStatus();

  const factory ListStatus.idle() = LIdle;

  const factory ListStatus.empty() = LEmpty;

  const factory ListStatus.loading() = LLoading;

  const factory ListStatus.success() = LSuccess;

  const factory ListStatus.error() = LError;

  const factory ListStatus.completed() = LCompleted;
}


class LIdle extends ListStatus {
  const LIdle();
}

class LEmpty extends ListStatus {
  const LEmpty();
}

class LLoading extends ListStatus {
  const LLoading();
}

class LCompleted extends ListStatus {
  const LCompleted();
}

class LSuccess extends ListStatus {
  const LSuccess();
}

class LError extends ListStatus {
  const LError();
}



